
package com.example.itext.PDF中设置样式;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;

import javax.lang.model.element.VariableElement;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class ITEXT {

    public static void main(String[] args) throws IOException, DocumentException {

        Document document = new Document();

        PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream("D:\\javaIdea\\IDEAPDF\\样式.pdf"));
        document.open();
        //字体文件
        BaseFont font = BaseFont.createFont("D:\\javaIdea\\itext\\src\\main\\resources\\static\\msyh.ttf", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);
        //创建字体
        Font font1 = new Font(font);
        // 设置字体的颜色
        font1.setColor(BaseColor.BLUE);
        font1.setSize(20);

        // 创建章节
        Paragraph elements = new Paragraph("段落标题xxxx", font1);
        Chapter elements1 = new Chapter(elements,30);
        elements1.setNumberDepth(1);

        Paragraph sectionTitle = new Paragraph("部分标题", font1);
        Section section1 = elements1.addSection(sectionTitle);


        Paragraph sectionContent = new Paragraph("部分内容", font1);
        section1.add(sectionContent);

        document.add(elements1);

        document.close();

        pdfWriter.close();

    }

}
